package com.yingluo.abacus.plantform.docs.app.hiberante;

import com.yingluo.abacus.boot.core.data.hibernate.dao.ICCDao;
import com.yingluo.abacus.boot.core.data.hibernate.utils.HibernateUtils;
import com.yingluo.abacus.plantform.docs.app.utils.DocsAppUtils;
import com.yingluo.abacus.plantform.docs.domain.app.hibenate.TestEntity;
import com.yingluo.abacus.plantform.utils.PlantformUtils;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;

/**
 * http://blog.csdn.net/catoop/article/details/50575038
 * 
 * @author sunzh
 *
 */
@RestController
public class SaveController implements Serializable {

	private final static String PATH= DocsAppUtils.MOUDLE_PATH+"/hibernate/save";
	private static final Logger logger = LoggerFactory.getLogger(SaveController.class);
	public static final long serialVersionUID = 1382303595861280282L;

	@RequestMapping(value = PlantformUtils.PROJECT_PATH  +PATH )
	public TestEntity execute() throws Exception {
		return service.save();
	}

	@Autowired
	@Qualifier( "service_"+SaveController.serialVersionUID )
	SaveService service;

}

@Service("service_"+SaveController.serialVersionUID)
class SaveService {
	@Autowired
	@Qualifier(HibernateUtils.DEFAULT_SESSIONFACTORY)
	private SessionFactory sessionFactory;

	@Transactional
	public TestEntity save() throws Exception {
		ICCDao<TestEntity, String> dao = (ICCDao<TestEntity, String>) HibernateUtils.getDao(sessionFactory,
				TestEntity.class);
		TestEntity po = new TestEntity();
		dao.saveLogic(po);

		if (false)
			throw new RuntimeException("error");
		return po;
	}

}
